package com.zzw.springboot.controller;


import com.zzw.springboot.bean.Inventory;
import com.zzw.springboot.bean.StockChange;
import com.zzw.springboot.service.InventoryService;
import com.zzw.springboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 库存控制器
 */
@RestController
@RequestMapping("/inventory")
public class InventoryController {

    @Autowired
    private InventoryService inventoryService;

    /**
     * 查询库存列表
     */
    @GetMapping("/list")
    public Result list(Inventory inventory) {
        List<Inventory> list = inventoryService.getInventoryList(inventory);
        return Result.success(list);
    }

    /**
     * 查询预警库存列表
     */
    @GetMapping("/alert")
    public Result alert() {
        List<Inventory> list = inventoryService.getAlertList();
        return Result.success(list);
    }

    /**
     * 查询指定商品的库存信息
     */
    @GetMapping("/detail")
    public Result detail(@RequestParam Integer productId, @RequestParam Integer warehouseId) {
        Inventory inventory = inventoryService.getInventoryByProductAndWarehouse(productId, warehouseId);
        if (inventory != null) {
            return Result.success(inventory);
        } else {
            return Result.error("库存记录不存在");
        }
    }

    /**
     * 设置库存预警阈值
     */
    @PutMapping("/setAlert")
    public Result setAlert(@RequestBody Map<String, Object> params) {
        Integer productId = (Integer) params.get("productId");
        Integer warehouseId = (Integer) params.get("warehouseId");
        Integer alertThreshold = (Integer) params.get("alertThreshold");

        boolean result = inventoryService.setAlertThreshold(productId, warehouseId, alertThreshold);
        if (result) {
            return Result.success("设置预警阈值成功");
        } else {
            return Result.error("设置预警阈值失败");
        }
    }

    /**
     * 手动入库
     */
    @PostMapping("/stockIn")
    public Result stockIn(@RequestBody Map<String, Object> params, HttpSession session) {
        Integer productId = (Integer) params.get("productId");
        Integer warehouseId = (Integer) params.get("warehouseId");
        Integer quantity = (Integer) params.get("quantity");
        String remark = (String) params.get("remark");

        // 获取当前用户ID
        Object userId = session.getAttribute("userId");
        if (userId == null) {
            return Result.error("未登录或会话已过期");
        }

        boolean result = inventoryService.stockIn(
            productId,
            warehouseId,
            quantity,
            (Integer) userId,
            null, // 手动操作，没有关联单据
            0,    // 0-手动操作
            remark
        );

        if (result) {
            return Result.success("入库操作成功");
        } else {
            return Result.error("入库操作失败");
        }
    }

    /**
     * 手动出库
     */
    @PostMapping("/stockOut")
    public Result stockOut(@RequestBody Map<String, Object> params, HttpSession session) {
        Integer productId = (Integer) params.get("productId");
        Integer warehouseId = (Integer) params.get("warehouseId");
        Integer quantity = (Integer) params.get("quantity");
        String remark = (String) params.get("remark");

        // 获取当前用户ID
        Object userId = session.getAttribute("userId");
        if (userId == null) {
            return Result.error("未登录或会话已过期");
        }

        boolean result = inventoryService.stockOut(
            productId,
            warehouseId,
            quantity,
            (Integer) userId,
            null, // 手动操作，没有关联单据
            0,    // 0-手动操作
            remark
        );

        if (result) {
            return Result.success("出库操作成功");
        } else {
            return Result.error("出库操作失败，请检查库存是否充足");
        }
    }

    /**
     * 库存调整
     */
    @PostMapping("/adjust")
    public Result adjust(@RequestBody Map<String, Object> params, HttpSession session) {
        Integer productId = (Integer) params.get("productId");
        Integer warehouseId = (Integer) params.get("warehouseId");
        Integer adjustQuantity = (Integer) params.get("adjustQuantity");
        String remark = (String) params.get("remark");

        // 获取当前用户ID
        Object userId = session.getAttribute("userId");
        if (userId == null) {
            return Result.error("未登录或会话已过期");
        }

        boolean result = inventoryService.adjustStock(
            productId,
            warehouseId,
            adjustQuantity,
            (Integer) userId,
            null, // 手动操作，没有关联单据
            3,    // 3-盘点单
            remark
        );

        if (result) {
            return Result.success("库存调整成功");
        } else {
            return Result.error("库存调整失败，请检查库存是否充足");
        }
    }

    /**
     * 查询库存变动记录
     */
    @GetMapping("/changes")
    public Result changes(StockChange stockChange) {
        List<StockChange> list = inventoryService.getStockChangeList(stockChange);
        return Result.success(list);
    }

    /**
     * 根据商品ID查询库存变动记录
     */
    @GetMapping("/changes/product/{productId}")
    public Result changesByProduct(@PathVariable Integer productId) {
        List<StockChange> list = inventoryService.getStockChangeByProductId(productId);
        return Result.success(list);
    }

    /**
     * 根据单据信息查询库存变动记录
     */
    @GetMapping("/changes/related")
    public Result changesByRelated(@RequestParam Integer relatedId, @RequestParam Integer relatedType) {
        List<StockChange> list = inventoryService.getStockChangeByRelated(relatedId, relatedType);
        return Result.success(list);
    }
}
